﻿using Game.NetWork;
using TemplatesDatabase;

namespace Game
{
    // 子系统天空岛地形，继承自地形子系统
    public class SubsystemSkyIslandTerrain : SubsystemTerrain
    {
        // 加载方法重写
        public override void Load(ValuesDictionary valuesDictionary)
        {
            // 查找并设置游戏组件子系统
            m_subsystemViews = base.Project.FindSubsystem<SubsystemGameWidgets>(throwOnError: true);
            base.SubsystemGameInfo = base.Project.FindSubsystem<SubsystemGameInfo>(throwOnError: true);
            m_subsystemParticles = base.Project.FindSubsystem<SubsystemParticles>(throwOnError: true);
            m_subsystemPickables = base.Project.FindSubsystem<SubsystemPickables>(throwOnError: true);
            m_subsystemBlockBehaviors = base.Project.FindSubsystem<SubsystemBlockBehaviors>(throwOnError: true);
            base.SubsystemAnimatedTextures = base.Project.FindSubsystem<SubsystemAnimatedTextures>(throwOnError: true);
            base.SubsystemFurnitureBlockBehavior = base.Project.FindSubsystem<SubsystemFurnitureBlockBehavior>(throwOnError: true);
            base.SubsystemPalette = base.Project.FindSubsystem<SubsystemPalette>(throwOnError: true);

            // 创建地形及其渲染器、更新器实例
            base.Terrain = new Terrain();
            base.TerrainRenderer = new TerrainRenderer(this);
            base.TerrainUpdater = new TerrainUpdater(this);

            // 根据工作类型选择地形序列化器
            if (CommonLib.WorkType != WorkType.Client)
            {
                base.TerrainSerializer = new TerrainSerializer23(base.SubsystemGameInfo.DirectoryName);
            }
            else
            {
                base.TerrainSerializer = new TerrainSerializerNet();
            }

            // 创建块几何生成器实例
            base.BlockGeometryGenerator = new BlockGeometryGenerator(base.Terrain, this, base.Project.FindSubsystem<SubsystemElectricity>(throwOnError: true), base.SubsystemFurnitureBlockBehavior, base.Project.FindSubsystem<SubsystemMetersBlockBehavior>(throwOnError: true), base.SubsystemPalette);

            // 根据游戏世界设置选择地形内容生成器
            if (string.CompareOrdinal(base.SubsystemGameInfo.WorldSettings.OriginalSerializationVersion, "2.1") <= 0)
            {
                TerrainGenerationMode terrainGenerationMode = base.SubsystemGameInfo.WorldSettings.TerrainGenerationMode;
                ITerrainContentsGenerator terrainContentsGenerator2;
                if (terrainGenerationMode != TerrainGenerationMode.FlatContinent && terrainGenerationMode != TerrainGenerationMode.FlatIsland)
                {
                    ITerrainContentsGenerator terrainContentsGenerator = new TerrainContentsGenerator21(this);
                    terrainContentsGenerator2 = terrainContentsGenerator;
                }
                else
                {
                    ITerrainContentsGenerator terrainContentsGenerator3 = new TerrainContentsGeneratorFlat(this);
                    terrainContentsGenerator2 = terrainContentsGenerator3;
                }

                base.TerrainContentsGenerator = terrainContentsGenerator2;
                return;
            }

            TerrainGenerationMode terrainGenerationMode2 = base.SubsystemGameInfo.WorldSettings.TerrainGenerationMode;
            ITerrainContentsGenerator terrainContentsGenerator5;
            if (terrainGenerationMode2 != TerrainGenerationMode.FlatContinent && terrainGenerationMode2 != TerrainGenerationMode.FlatIsland && terrainGenerationMode2 != TerrainGenerationMode.Nihility)
            {
                ITerrainContentsGenerator terrainContentsGenerator4 = new TerrainContentsGenerator23(this);
                terrainContentsGenerator5 = terrainContentsGenerator4;
            }
            else
            {
                ITerrainContentsGenerator terrainContentsGenerator6 = new TerrainContentsGeneratorFlat(this);
                terrainContentsGenerator5 = terrainContentsGenerator6;
            }

            // 如果种子包含 "kd"，则选择天空岛地形内容生成器
            if (base.SubsystemGameInfo.WorldSettings.Seed.ToLower().Contains("kd"))
            {
                ITerrainContentsGenerator terrainContentsGenerator7 = new TerrainSkyIslandContentsGenerator23(this);
                terrainContentsGenerator5 = terrainContentsGenerator7;
            }

            base.TerrainContentsGenerator = terrainContentsGenerator5;
        }
    }
}
